package br.ufrgs.tsp.model;

import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Encapsulates a TSP city data and operations.
 * A city structure is static defined by an assossiation matrix before the program is started.
 * Becouse of that, the cities attributes are <code>final</code>, these ones cannot be changed at run time.
 * 
 * @author mineiro
 */
public class City implements ICity {
	
	/**
	 * Neigboards cities.
	 */
	private final List<ICity> neigboards = new LinkedList<ICity>();
	
	/**
	 * City identifyer.
	 */
	private final int cityId;
	
	protected City(int cityId) {
		this.cityId = cityId;
	}
	
	/**
	 * Constructor.
	 * @param cityId city identifyer
	 * @param cities list of neigboards cities
	 */
	protected City(int cityId, List<ICity> cities) {
		if (cities == null)
			throw new InvalidParameterException("The parameters cannot be null!");
		
		this.cityId = cityId;
		this.neigboards.addAll(cities);
	}

	/*
	 * (non-Javadoc)
	 * @see br.ufrgs.ga.tsp.model.ICity#hasNeigboards()
	 */
	public boolean hasNeigboards() {
		return this.neigboards.size() > 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.ufrgs.ga.tsp.model.ICity#neigboardsCount()
	 */
	public int getNeigboardsCount() {
		return this.neigboards.size();
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.ufrgs.ga.tsp.model.ICity#getNeigboards()
	 */
	public List<ICity> getNeigboards() {
		return neigboards;
	}

	/*
	 * (non-Javadoc)
	 * @see br.ufrgs.ga.tsp.model.ICity#getCityId()
	 */
	public int getCityId() {
		return cityId;
	}

	@Override
	public ICity getNeigboardAt(int index) {
		if (index < 0) {
			return neigboards.get(0);
		} else if (index >= neigboards.size()) {
			return neigboards.get(neigboards.size() - 1);
		}
		return neigboards.get(index);
	}
	
	public void addNeigboard(ICity... city) {
		this.neigboards.addAll(Arrays.asList(city));
	}
	
	public boolean hasNeigboard(ICity city) {
		return this.neigboards.contains(city);
	}
	
	@Override
	public String toString() {
		return Integer.toString(cityId);
	}
}
